package com.jwj.controller;

import com.jwj.pojo.Account;
import com.jwj.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
//@CrossOrigin(origins="*",maxAge = 3600)
public class UserContrller {

    @Resource
    private UserService userService;

    //管理员登录
    @PostMapping(value = "/login")
    @ResponseBody
    public Account loginOne(@Param("username") String username,@Param("password") String password){
        Account account = userService.loginOne(username);
        System.out.println(username);
        System.out.println(password);
        //获取当前用户名
        Subject subject=SecurityUtils.getSubject();
        //封装登录用户的数据
        UsernamePasswordToken token =new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            return account;
        }catch (UnknownAccountException e){
            System.out.println(e);
            return null;
        }catch (IncorrectCredentialsException e){
            System.out.println(e);
            return null;
        }
    }
    //根据id查找
    @GetMapping("/loginOneId")
    @ResponseBody
    public Account loginOneId(@Param("account_id") Integer account_id){
        Account account = userService.loginOneId(account_id);
        System.out.println(account_id);
        return account;
    }
//    @RequestMapping("/loginOut")
//    public String loginOut(HttpSession session){
//        Subject subject = SecurityUtils.getSubject();
//        try {
//            subject.logout();
//            session.invalidate();
//            System.out.println("清除");
//            return "redirect:/";
//        }catch (Exception e){
//            System.out.println(e);
//            System.out.println("已经清除");
//            return "redirect:/";
//        }
//    }

//    @RequestMapping(value = "/testlogin")
//    public String testlogin(String username, String password,
//                                ModelAndView modelAndView){
////        Account loginOne = userService.loginOne(username, password);
//        //获取当前的用户
//        System.out.println(username);
//        System.out.println(password);
//        Subject subject=SecurityUtils.getSubject();
//        //封装用户的登录数据
//        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
//        try {
//            subject.login(token);//执行登录方法，如果没有异常就ok
//            Subject subject1 = SecurityUtils.getSubject();
//            return "admin_index";
//        }catch (UnknownAccountException e){//用户名不存在
//            modelAndView.addObject("msg","用户名错误");
//            System.out.println(e);
//            return "test";
//        }catch (IncorrectCredentialsException e){//密码不存在
//            modelAndView.addObject("msg","密码错误");
//            System.out.println(e);
//            return "test";
//        }
//    }

}
